**********************************************************
* 			Master analysis file for do files	
*														 
* To compile all exhibits generated by Stata in the paper		 
* This do-file calls all individual do-files 	 
* The global macro "root" will have to be set			 
* All individual do-files require only this macro to run  
**********************************************************

set more off
clear all
clear mata
program drop _all
capture log close
set logtype text
clear matrix
*set maxvar 10000
set memory 500m
set trace off
eststo clear

*Additional packages to install if not already installed: 
/*
ssc install estout
ssc install dsconcat
ssc install qqvalue
ssc install texsave
ssc install dm79

*** For parmest
net from "http://www.rogernewsonresources.org.uk/"
* click "stata11"
* click "parmest"
* click "clikc here to install"

*** For svmat2
findit svmat2
* click dm79 from http://www.stata.com/stb/stb56


*/

*set user directory; please set the macro "root" to the base directory of the directory containing the extracted content of the zip-file
glo root "C:\Users\tbrai\Dropbox\DBT\Data\ePOS RCT Jharkhand\ABBA replication package_20200814"   //
di in red "The root directory is set to ${root}"


*set directories to load data, user-defined functions/programs/ado files, and store output  
glo AdminDataDir "${root}/Data/Admin"
glo SurveyDataDir "${root}/Data/Survey"
glo OutputDir "C:\Users\tbrai\Dropbox\github_private\ABBA\replication\exhibits"
glo CodeDir "C:\Users\tbrai\Dropbox\github_private\ABBA\replication\code"
glo functionDir "${root}/AnalysisCode/Lib/Functions"
glo adoDir "${root}/AnalysisCode/Lib/Ado"


*load programs/functions files written by RAs working on this project
*qui include "${functionDir}/AttritionTable_program_svy.do"
qui include "${functionDir}/AttritionTable_program.do"
qui include "${functionDir}/AttritionTable_program_svy.do"
qui include "${functionDir}/StackingRegressionModels.do"
qui include "${functionDir}/Preferences_Perceptions_program.do"
qui include "${functionDir}/Balance_program.do"
qui include "${functionDir}/newformatadjustedTEs.do"



log using "${OutputDir}/analysis_logfile.txt", replace 

timer on 1 

*****************************************************************
* Table 1 - Baseline balance and program implementation
* This table compares treatment to control blocks within study districts on baseline characteristics and and measures of program implementation 
do "${CodeDir}/Table1.do"


**
* Table 2 - Effects on value disbursed, value received, and leakage
* This table reports estimated treatment effects on the value of commodities disbursed by the government, received by recipients, and the difference in endline one (January - March).
do "${CodeDir}/Table2.do"


**
* Table 3: Effects on transaction costs
* This table reports estimated treatment effects on measures of transaction costs incurred transferring PDS commodities
do "${CodeDir}/Table3.do"


**
* Table 4: Effects on the extensive margin of value received
* This table reports estimated treatment effect on the extensive margin of the values received by beneficiaries per month in endline one
do "${CodeDir}/Table4.do"



**
*Table 5: Heterogeneous effects by Aadhaar seeding
*This table reports estimated differential treatment effects by Aadhaar seeding status in endline one 
do "${CodeDir}/Table5.do"



**
* Table 6: Effects on dealer expectations
* This table reports estimated treatment effects on measures of Fair Price Shop dealers' expectations in March about the future.
do "${CodeDir}/Table6.do"



**
* Table 7: Effects of reconciliation
* This table reports estimates of the effect of reconciliation on measures of the value disbursed by the governmen received by recipients, and the dfference separately for treatment and control areas
do "${CodeDir}/Table7.do"



**
* Table 8: Decomposition of ration card deletions
* This table reports the decomposition of ration card deletions by Aadhaar seeding status
do "${CodeDir}/Table8.do"	 
		 
**
*Table A.2: Comparison of dealer samples
*This table compares the PDS dealers originally sampled at baseline ("original sample") with those added at the first endline as a result of ration card re-assignment across ration shops ("additional sample")
do "${CodeDir}/TableA_2.do"


**
*Table A.3: Outcome missingness by treatment status: all households
*This table reports the rate at which various household outcomes measured in endline one (January - March) are not observed, by treatment status. We include all surveyed households and all households categorized as "ghosts"
do "${CodeDir}/TableA_3.do"

**
*Table A.4: Outcome missingness by treatment status: surveyed households
*This table reports the rate at which various household outcomes measured in endline one (January - March) are not observed, by treatment status. We include only surveyed households
do "${CodeDir}/TableA_4.do"


**
*Table A.5: Effects on quantity disbursed, quantity received, and leakage
*This table reports estimated treatment effects on the quantity of commodities disbursed by the government, received by recipients, and the difference in endline one (January - March)
do "${CodeDir}/TableA_5.do"


**
*Table A.6: Effects on market prices and overcharges
*This table reports estimated treatment effects on the market prices reported by beneffciaries, market prices reported by FPS dealers, statutory prices, and total overcharges (Panel D) in endline one (January - March)
do "${CodeDir}/TableA_6.do"

**
* Table A.7 - Effects on quality of ration received
* This table reports estimated treatment effects on the quality of commodities received by beneficiaries in March 2017 
do "${CodeDir}/TableA_7.do"


**
* Table A.8: Effects on food security
* This table reports estimated treatment effects on measures of food security in March
do "${CodeDir}/TableA_8.do"


**
*Table A.9: Effects on determinants of beneficiary transaction costs
*This table reports estimated treatment effects on the costs incurred by beneffciaries to access PDS rations in March 2017
do "${CodeDir}/TableA_9.do"

**
*Table A.10: Heterogeneous effects by household characteristics
*This table reports differential estimated treatment effects along dimensions of household characteristics for ration card-level outcomes in endline one (January- March)
do "${CodeDir}/TableA_10.do"


**
*Table A.11: Heterogeneous effects by location characteristics
*This table reports differential estimated treatment effects along dimensions of location characteristics for ration card-level outcomes in endline one (January- March)
do "${CodeDir}/TableA_11.do"


**
*Table A.12: Heterogeneous effect by machine mode
*This table reports differential estimated treatment effects by machine mode assigned to dealers per month for ration card-level outcomes in endline one (January - March)
do "${CodeDir}/TableA_12.do"


**
*Table A.14: Heterogeneous effects of reconciliation on value disbursed and received by FPS balance
*This table reports differential effects of accumulated stock balance at the time of reconciliation onset (July 2017) on value disbursed by dealers and received by beneffciaries
do "${CodeDir}/TableA_14.do"

**
*Figure A.3: Household expected income and years of schooling by seeding status
*This figure shows the distribution of measures of household income and education by whether household had at least one Aadhaar number seeded at baseline
do "${CodeDir}/FigureA_3.do"

**
*Figure A.5: Recorded grain stock as of June 2017, by treatment status
*This figure shows the distribution of grain (i.e. rice and wheat) in kilograms held by FPSs at the end of June 2017 according to government records, separately for shops in treated and control blocks
do "${CodeDir}/FigureA_5.do"

**
*Table B.1: Effects on value received using alternative specifications
*This table reports alternative specifications for Panel B in Table 3 by reporting models with March data only, pooled data with a linear trend, and pooled data without baseline lag
do "${CodeDir}/TableB_1.do"

**
*Table B.2: Effects on value received using alternative specifications
*This table reports alternative specifications for Panel B in Table A.4 by reporting models with March data only, pooled data with a linear trend, and pooled data without baseline lag
do "${CodeDir}/TableB_2.do"

**
*Table B.3: Effects on value received using alternative specifications
*This table reports alternative specifications for Panel B in Table A.5 by reporting models with March data only, pooled data with a linear trend, and pooled data without baseline lag
do "${CodeDir}/TableB_3.do"

**
*Table B.4
* This table reports a robustness check to Panel B of Table 3 by measuring value as the amount a household is willingness to accept in lieu of ration
do "${CodeDir}/TableB_4.do"


**
* Table B.5: Effects on dealer expectations controlling for baseline values
* This table reports estimated treatment effects on measures of Fair Price Shop dealers' expectations in March about the future while controlling for baseline values.
do "${CodeDir}/TableB_5.do"


**
* Table B-6: Perceptions of the ePOS intervention
* This table reports summary statistics of households' and FPS dealers' stated preferences for and perceptions of the ePOS intervention in March
do "${CodeDir}/TableB_6.do"


**
*Figure D.3: Adherence to reconciliation protocol
* This figure plots the evolution of actual grain disbursements per FPS against the counterfactual time series obtained by recalculating disbursements assuming that the government implemented reconciliation strictly and while leaving unchanged the reported take of grain
do "${CodeDir}/FigureD_3.do"

timer off 1 

timer list 1

log close 
